package com.ruoyi.cs.event.supplieroffer.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.event.supplieroffer.domain.SupplierOffer;
import com.ruoyi.cs.event.supplieroffer.domain.SupplierOfferList;
import com.ruoyi.cs.event.supplieroffer.mapper.SupplierOfferMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 供应商报价
 * 业务层执行
 * @author Drj 2021/6/28
 */
@Service
public class SupplierOfferServiceImpl implements ISupplierOfferService {
    @Autowired
    private SupplierOfferMapper supplierOfferMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 供应商报价查询
     *
     * @param supplierOffer 查询条件
     * @return
     */
    @Override
    public List<SupplierOffer> selectSupplierOfferList(SupplierOffer supplierOffer) {
        List<SupplierOffer> supplierOffers = supplierOfferMapper.selectSupplierOfferList(supplierOffer);
        for (int i = 0; i < supplierOffers.size(); i++) {
            supplierOffers.get(i).setSupplierOfferLists(supplierOfferMapper.selectSupplierOfferListListById(supplierOffers.get(i).getSupplierQuotaId()));
        }
        return supplierOffers;
    }

    /**
     * 供应商报价根据编号查询
     *
     * @param supplierQuotaId
     * @return
     */
    @Override
    public SupplierOffer selectSupplierOfferById(String supplierQuotaId) {
        return supplierOfferMapper.selectSupplierOfferById(supplierQuotaId);
    }

    /**
     * 新增供应商报价
     *
     * @param supplierOffer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSupplierOffer(SupplierOffer supplierOffer) throws Exception{
        if (StringUtils.isNotNull(supplierOfferMapper.selectSupplierOfferById(supplierOffer.getSupplierQuotaId()))){
            //存在
            return 0;
        }
        //不存在
        String supplierQuotaId = idUtil.createSupplierQuotaId();
        supplierOffer.setSupplierQuotaId(supplierQuotaId);
        supplierOfferMapper.insertSupplierOffer(supplierOffer);
        for (int i = 0; i < supplierOffer.getSupplierOfferLists().size(); i++) {
            supplierOffer.getSupplierOfferLists().get(i).setSupplierQuotaId(supplierQuotaId);
            supplierOfferMapper.insertSupplierOfferList(supplierOffer.getSupplierOfferLists().get(i));
        }
        return 1;
    }

    /**
     * 更新供应商报价
     *
     * @param supplierOffer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSupplierOffer(SupplierOffer supplierOffer)throws Exception {
        if (!StringUtils.isNotNull(supplierOfferMapper.selectSupplierOfferById(supplierOffer.getSupplierQuotaId()))){
            //不存在
            return 0;
        }
        supplierOfferMapper.updateSupplierOffer(supplierOffer);
        //现有数据库内的
        List<SupplierOfferList> SupplierOfferListsOld = supplierOfferMapper.selectSupplierOfferListListById(supplierOffer.getSupplierQuotaId());
        //更新部分
        List<SupplierOfferList> SupplierOfferListsNew = supplierOffer.getSupplierOfferLists();
        for (int i = 0; i < SupplierOfferListsNew.size(); i++) {
            //查询数据库此条供应商报价详细是否存在
            SupplierOfferList supplierOfferList = supplierOfferMapper.selectSupplierOfferListById(SupplierOfferListsNew.get(i));
            if (StringUtils.isNotNull(supplierOfferList)){
                //存在
                for (int j = 0; j < SupplierOfferListsOld.size(); j++) {
                    if (SupplierOfferListsOld.get(j).getProduceId().equals(supplierOfferList.getProduceId())){
                        //移除旧有中相同部分
                        SupplierOfferListsOld.remove(i);
                        break;
                    }
                }
                //更新
                supplierOfferMapper.updateSupplierOfferList(SupplierOfferListsNew.get(i));
            }else {
                //不存在，新增
                supplierOfferMapper.insertSupplierOfferList(SupplierOfferListsNew.get(i));
            }
        }
        for (int i = 0; i < SupplierOfferListsOld.size(); i++) {
            supplierOfferMapper.deleteSupplierOfferListById(SupplierOfferListsOld.get(i));
        }
        return 1;
    }

    /**
     * 删除供应商报价
     * 仅修改删除标志
     *
     * @param supplierOffer
     * @return
     */
    @Override
    public int updateSupplierOfferDeleteFlag(SupplierOffer supplierOffer) {
        return supplierOfferMapper.updateSupplierOfferDeleteFlag(supplierOffer);
    }

}
